﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ReactiveUI.Fody.Helpers;

namespace Common.ViewModels
{
    /// <summary>
    /// 小车(这里小车,小车的移动按照通道来,小车不能决定自己的方向)
    /// </summary>
    [Serializable]
    [JsonObject(MemberSerialization.OptOut)]
    public class Car: LineRigidBody
    {

        public  Car()
        {
            VisualMoveCollector = new VisualMoveCollector(this);
        }

        [JsonIgnore]
        public VisualMoveCollector VisualMoveCollector { get; set; }

        /// <summary>
        /// 槽位的位置(小车槽位)
        /// </summary>
        [Reactive] public Point3D Point3D { get; set; }=new ();

        [Reactive] public LineRigidBody 小车自带池子 { get; set; } = new LineRigidBody();

        /// <summary>
        /// 机械手位置
        /// </summary>
        [Reactive] public Point3D 机械手位置 { get; set; }


        [Reactive] public CarStatus Status { get; set; }

        /// <summary>
        /// 是否有工件
        /// </summary>
        [JsonIgnore]
        [Reactive] public bool Have工件 { get; set; } = false;

        /// <summary>
        /// 小车当前工件
        /// </summary>
        [Reactive]
        [JsonIgnore]
        public 工件 工件 { get; set; }

        ///// <summary>
        ///// 当前位置(可解算为世界坐标系)
        ///// </summary>
        //[Reactive]
        //[JsonIgnore]
        //public double Position {  get; set; }

        /// <summary>
        /// 当前的摆渡
        /// </summary>
        [Reactive]
        [JsonIgnore]
        public BoatVM CurrentBoatVM { get; set; }
        
        
        /// <summary>
        /// 移动到指定位置(这里不涉及到进入主通道)
        /// </summary>
        /// <param name="fromStation"></param>
        /// <param name="toStation"></param>
        public void MoveStation(Station fromStation,Station toStation)
        {
            //这里其实是让他的父节点运动
            

            //if(toStation.GetCanMoveAnd())
        }

        /// <summary>
        /// 将小车2移动到指定站点
        /// </summary>
        /// <param name="toStation"></param>
        public void MoveStation(double pos)
        {
            VisualMoveCollector.CmdMoveAbs(pos,ProductLine.Instance.CarSpeed,out _);
        }

        private readonly object _lock = new object ();

        /// <summary>
        /// 锁住的小车不能调度
        /// </summary>
        [Reactive]public bool IsLock { get;  set; } = false;

        /// <summary>
        /// 将小车移动到指定站点
        /// </summary>
        /// <param name="toStation"></param>
        public void MoveStationX(Station toStation)
        {
            //这里其实是让摆渡的父容器进行运动
            //这里移动会产生通道的边锁住的情况。即当前没有车,但是路径会被锁死

        }

        /// <summary>
        /// 将小车移动到指定站点(Y方向)
        /// </summary>
        /// <param name="toStation"></param>
        public void MoveStationY(Station toStation)
        {
            //让小车自身进行运动
        }

        /// <summary>
        /// 获取当前小车
        /// </summary>
        /// <returns></returns>
        public bool GetCanUse()
        {
            if (this.IsLock)
            {
                return false;
            }
            lock (_lock)
            {
                if (this.IsLock)
                {
                    return false;
                }
                this.IsLock = true;
                return false;
            }
        }


        /// <summary>
        /// 释放锁
        /// </summary>
        public void ReleaseLock()
        {
            lock (_lock)
            {
                this.IsLock = false;
            }
        }

    }
}
